package com.flame.session.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.flame.auth.core.constants.AuthConstants;
import com.flame.auth.core.entity.TrustedPrincipal;
import com.flame.session.controller.vo.OnlineUserVo;
import com.flame.session.entity.OnlineUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.session.Session;
import org.springframework.session.SessionRepository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

public class JdbcOnlineUserServiceImpl implements OnlineUserService {

    @Autowired
    private SessionRepository sessionRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<OnlineUser> list(OnlineUserVo onlineUserVo) {
        List<OnlineUser> onlineUserList =
            jdbcTemplate.query("SELECT PRIMARY_ID, SESSION_ID from SPRING_SESSION", (rs, rowNum) -> {
                OnlineUser onlineUser = new OnlineUser();
                onlineUser.setPrimaryId(rs.getString(1));
                onlineUser.setSessionId(rs.getString(2));
                return onlineUser;
            });

        if (CollectionUtils.isEmpty(onlineUserList)) {
            return new ArrayList<>();
        }

        for (OnlineUser onlineUser : onlineUserList) {
            Session session = sessionRepository.findById(onlineUser.getSessionId());
            if (session == null) {
                continue;
            }
            TrustedPrincipal trustedPrincipal = session.getAttribute(AuthConstants.TRUSTED_PRINCIPAL);
            if (trustedPrincipal != null) {
                onlineUser.setAccount(trustedPrincipal.getAccount());
                onlineUser.setUserName(trustedPrincipal.getUserName());
                onlineUser.setOrgCode(trustedPrincipal.getOrgCode());
                onlineUser.setIp(trustedPrincipal.getIp());
            } else {
                onlineUser.setAccount("无");
                onlineUser.setUserName("游客");
            }
        }

        return onlineUserList.stream().filter(onlineUser -> {
            if (StringUtils.hasText(onlineUserVo.getUserId())) {
                return onlineUser.getAccount().contains(onlineUserVo.getUserId());
            }
            return true;
        }).filter(onlineUser -> {
            if (StringUtils.hasText(onlineUserVo.getUserName())) {
                return onlineUser.getUserName().contains(onlineUserVo.getUserName());
            }
            return true;
        }).collect(Collectors.toList());
    }

    @Override
    public void forceLogout(String primaryId) {
        if (StringUtils.hasText(primaryId)) {
            jdbcTemplate.update("DELETE FROM SPRING_SESSION WHERE PRIMARY_ID = ?", primaryId);
            jdbcTemplate.update("DELETE FROM SPRING_SESSION_ATTRIBUTES WHERE SESSION_PRIMARY_ID = ?", primaryId);
        }
    }
}
